A Narcissistic Number is a number which is the sum of its own digits, each raised to the power of the number of digits.
For example, take 153 (3 digits):
1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
and 1634 (4 digits):
1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634
Your code must return true or false depending upon whether the given number is a Narcissistic number.
Error checking for text strings or other invalid inputs is not required, only valid integers will be passed into the function.
In [25]:
from functools import reduce
def narcissistic( value ):
num = int(value)
numL = []
while num > 0:
numL.append(num % 10)
num /= 10
narc = reduce(lambda x,y: x+y ,map(lambda x: x**len(numL), numL))
return value == narc
In [28]:
narcissistic(1633)
Out[28]:
In [33]:
def better_narc( value ):
print value==sum([int(i)**len(str(value)) for i in str(value)])
In [34]:
better_narc(153)
In [56]:
word = "AWUBWUBWUBBWUBWUBWUBC"
word = word.split("WUB")
word = [i for i in word if i != '']
word = ' '.join(word)
print word
In [64]:
word1 = "AWUBWUBWUBBWUBWUBWUBC".split("WUB")
word1 = ' '.join([i for i in word1 if i != ''])
print word1
In [67]:
def song_decoder(song):
nowubs = song.split("WUB")
return ' '.join([i for i in nowubs if i])
def better_song_decoder(song):
return " ".join(song.replace('WUB', ' ').split())
In [66]:
song_decoder("AWUBWUBWUBBWUBWUBWUBC")
Out[66]:
In [70]:
"AWUBWUBWUBBWUBWUBWUBC".replace('WUB', ' ').split()
Out[70]:
In [ ]: